package cn.net.isir.controller.manager;

import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.core.BasicController;
import cn.net.isir.core.SetBasicService;
import cn.net.isir.entity.*;
import cn.net.isir.service.*;
import cn.net.isir.utils.JSONUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename RoleController.java
 * @package cn.net.isir.controller.manager
 * @email lxb@isir.net.cn
 * @date 2019/02/25
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BasicController<Role> {
    @SetBasicService
    @Resource
    private RoleService roleService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private RolePermissionService rolePermissionService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private UserService userService;

    @Override
    public ResultObject add(@RequestBody Map map) {
        Role role = JSONUtils.toEntityObject(map, Role.class);
        Role role_new = new Role();
        role_new.setRoleName(role.getRoleName());
        role_new = roleService.selectOne(role_new);
        if (role_new != null) {
            ResultObject ro = new ResultObject();
            ro.setResult(505);
            ro.setMessage("roleName[" + role.getRoleName() + "] is already exist!");
            return ro;
        }
        return super.add(map);
    }

    /**
     * 指定角色,获取该角色的权限树
     *
     * @param id 角色id
     * @return 权限树
     */
    @RequestMapping(value = "/permissions/{id}", method = RequestMethod.GET)
    public ResultObject getPermissions(@PathVariable String id) {
        RolePermission rp = new RolePermission();
        rp.setRoleId(id);
        List<RolePermission> rolePermissionList = rolePermissionService.selectList(rp);
        List<Permission> permissionList = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            permissionList.add(permissionService.selectByPrimaryKey(rolePermission.getPermissionId()));
        }
        Collections.reverse(permissionList);
        return new ResultObject(200, "success", permissionList);
    }

    /**
     * 获取当前用户的权限列表
     *
     * @return 权限列表
     */
    @RequestMapping(value = "/mypermissions", method = RequestMethod.GET)
    public ResultObject getMyPermissions() {
        // 获取当前用户的角色
        String userId = TokenManager.getTokenId();
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole = userRoleService.selectOne(userRole);
        // 根据当前用户角色获取权限列表
        RolePermission rp = new RolePermission();
        rp.setRoleId(userRole.getRoleId());
        List<RolePermission> rolePermissionList = rolePermissionService.selectList(rp);
        List<Permission> permissionList = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            permissionList.add(permissionService.selectByPrimaryKey(rolePermission.getPermissionId()));
        }
        Collections.reverse(permissionList);
        return new ResultObject(200, "success", permissionList);
    }



    @Override
    public ResultObject delete(@PathVariable String id) {
        UserRole userRole = new UserRole();
        userRole.setRoleId(id);
        List<UserRole> userList = userRoleService.selectList(userRole);
        if(null!=userList&&userList.size()>0){
            return new ResultObject(505, "error", "There are users under the role");
        }
        return super.delete(id);
    }
}
